<%@page import="org.vehicle.common.MyConstant"%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html>
<%
    String path = request.getContextPath();
%>
<html xmlns="http://www.w3.org/1999/xhtml">
	<div id="page-wrapper">
        <div id="page-inner">
            <div class="row">
                <div class="col-md-12">
                    <!-- Advanced Tables -->
                    <div class="panel panel-default">
                        <div class="panel-heading">管理员信息列表</div>
                        <div class="panel-body">
                            <form class="form-horizontal">
                                <div class="form-group">
                                    <label for="vehicleLicense" class="col-md-2 control-label">管理员姓名</label>
                                    <div class="col-md-2">
                                        <input type="text" class="form-control" id="managerName" placeholder="管理员姓名">
                                    </div>
                                    <label for="personName" class="col-md-2 control-label">身份证号</label>
                                    <div class="col-md-2">
                                        <input type="text" class="form-control" id="certificateCode" placeholder="身份证号">
                                    </div>
                                    <div class="col-md-2">
                                        <a type="search" onclick="load(1)" class="btn btn-primary">搜索</a>
                                    </div>
                                </div>
                            </form>

                            <div id="toolbar" class="btn-group">
                                <button id="btn_add" type="button" class="btn btn-default" data-toggle="modal"
                                        data-target="#myModal">
                                    <span class="glyphicon glyphicon-plus" aria-hidden="true"></span>新增
                                </button>
                            </div>
 							<div id="toolbar" class="btn-group">
                                <button id="btn_add" type="button" class="btn btn-default" data-toggle="modal"
                                        data-target="#myModal2">
                                    <span class="glyphicon glyphicon-plus" aria-hidden="true"></span>导入管理员
                                </button>
                            </div>
                            <div style="height:20px"></div>
                            <div class="table-responsive">
                                <table class="table table-striped table-bordered table-hover"
                                       id="dataTables-example">
                                    <thead>
                                    <tr>
                                        <th>序号</th>
                                        <th>管理员姓名</th>
                                        <th>管理员身份证号</th>
                                        <th>管理员手机号</th>
                                        <th>管理员工号</th>
                                        <th>所属管理站</th>
                                        <th>时间</th>
                                        <th>操作</th>
                                    </tr>
                                    </thead>
                                    <tbody id="tbody">
                                    </tbody>
                                </table>
                                <div style="float:right" id="page" class="tr"></div>
                            </div>

                        </div>
                    </div>
                    <!--End Advanced Tables -->
                </div>
            </div>
        </div>
    </div>

<div class="modal fade" id="myModal" tabindex="-1" role="dialog"
     aria-labelledby="myModalLabel" aria-hidden="true" data-backdrop="static" data-keyboard="false">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal"
                        aria-hidden="true">&times;</button>
                <h4 class="modal-title" id="myModalLabel"></h4>
            </div>
            <div class="modal-body">
                <form action="" id="managerinfo">
                    <div class="form-body pal">
                        <div class="row">
                            <div class="col-md-6">
                                <div class="form-group">
                                    <label for="" class="control-label">管理员姓名</label>
                                    <div class="">
                                        <input id="managerName1" type="text" name="managername"
                                               class="form-control"></div>
                                    <input id="managerinfoid" name="managerinfoid"
                                           style="display: none">
                                </div>
                            </div>
                            <div class="col-md-6">
                                <div class="form-group">
                                    <label for="" class="control-label">所属管理站</label>
                                    <c:if test="${manager.managerType == 1001}">
                                    <select id="organizationInfoId" name="organizationInfoId" class="form-control">
                                        <option value="">----</option>
                                    </select>
                                    </c:if>
                                    <c:if test="${manager.managerType == 1002}">
                                    <select class="form-control" disabled="disabled">
                                        <option value="">${manager.organizationName }</option>
                                    </select>
                                    <input type="hidden" id="organizationInfoId" name="organizationInfoId" value="${manager.organizationInfoId }">
                                    </c:if>
                                </div>
                            </div>

                        </div>
                        <div class="row">
                            <div class="col-md-6">
                                <div class="form-group">
                                    <label for="" class="control-label">工号</label>
                                    <div class="">
                                        <input id="jobnumber" type="text" name="jobnumber"
                                               class="form-control"></div>
                                </div>
                            </div>
                            <div class="col-md-6">
                                <div class="form-group">
                                    <label for="" class="control-label">身份证号</label>
                                    <div class="">
                                        <input id="certificatecode" type="text" name="certificatecode"
                                               class="form-control"></div>
                                </div>
                            </div>
                        </div>
                        <div class="row">
                            <div class="col-md-6">
                                <div class="form-group">
                                    <label for="" class="control-label">手机号</label>
                                    <div class="">
                                        <input id="mobilephone" type="text" name="mobilephone"
                                               class="form-control"></div>
                                </div>
                            </div>
                            <div class="col-md-6">
                                <div class="form-group">
                                    <label for="" class="control-label">登录名</label>
                                    <div class="">
                                        <input id="loginname" type="text" name="loginname"
                                               class="form-control"></div>
                                </div>
                            </div>
                        </div>
                    </div>
                </form>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">关闭
                </button>
                <button id="managerInfoButton" type="button" class="btn btn-primary">提交
                </button>
            </div>
        </div>
        <!-- /.modal-content -->
    </div>
</div>
<!-- /.modal -->


<div class="modal fade" id="myModal2" tabindex="-1" role="dialog"
     aria-labelledby="myModalLabel" aria-hidden="true" data-backdrop="static" data-keyboard="false">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal"
                        aria-hidden="true">&times;</button>
                <h4 class="modal-title" id="myModalLabel">导入管理员</h4>
            </div>
            <div class="modal-body">
				<form id="excelForm" class="form-horizontal"
					action="<%=path%>/importManager" method="post"
					enctype="multipart/form-data">
					<div class="form-group">
						<div class="col-md-1"></div>
						<div class="col-md-2">
						 <c:if test="${manager.managerType == 1001}">
			                   <button type="button" class="btn btn-default"
								onclick='location.href = "<%=path%>/template/managerTemplate.xlsx"'>下载模板</button>
			             </c:if>
			             <c:if test="${manager.managerType == 1002}">
			                 <button type="button" class="btn btn-default"
								onclick='location.href = "<%=path%>/template/managerTemplate2.xlsx"'>下载模板</button>
			             </c:if>
						</div>
						<div class="col-md-5">
							<input type="text" id="fileName" name="fileName"
								class="form-control" readonly="readonly"
								onclick="$('#excelFile').click();" placeholder="选择导入的Excel" /> <input
								type="file" id="excelFile" name="excelFile" style="display: none;"
								onchange="$('#fileName').val(this.value)" />
						</div>
						<div class="col-md-4">
							<button type="button" id="importButton" class="btn btn-default" onclick="managerImport()">导入模板</button>
						</div>
						<div class="col-md-12">
							<div class="panel-body table-responsive">
							<font color="red">默认密码：<%=MyConstant.DEFAULT_PASSWORD %></font>
								<table class="table table-bordered" style="font-size: 5px;">
									<thead>
										<tr>
											<th>行号</th>
											<th>管理员姓名</th>
											 <c:if test="${manager.managerType == 1001}">
			                                    <th>所属管理站</th>
			                                </c:if>
											<th>工号</th>
											<th>管理员身份证号</th>
											<th>手机号</th>
											<th>登录名</th>
											<th>导入结果</th>
										</tr>
									</thead>
									<tbody id="importTB">
										
									</tbody>
								</table>
							</div>
						</div>
					</div>
				</form>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">关闭
                </button>
                <button id="managerInfoButton" type="button" class="btn btn-primary">提交
                </button>
            </div>
        </div>
        <!-- /.modal-content -->
    </div>
</div>
<!-- /.modal -->
</body>
<script>
	var managerType='${manager.managerType}';
    $(document).ready(function () {
        load(1);
        initData()
    });
    
    
    function valContent(){
        return $('#managerinfo').validate({
            rules: {
            	managername: {
                    required: true
                },
                organizationInfoId: {
                    required: true
                },
                certificatecode: {
                	isCertificateCode: true,
                	required: true
                },
                loginname: {
                	isLoginName: true,
                	required: true
                }
            },
            highlight: function(element) {
                $(element).closest('.form-group').removeClass('success').addClass('error');
            },
            success: function(element) {
                element.text('OK!').addClass('valid')
                    .closest('.form-group').removeClass('error').addClass('success');
            }
        }).form();
    }
    
    //添加修改
    $("#managerInfoButton").on('click', function (e) {
    	if(valContent()){
    		var managername = $("#managerName1").val();
            var managerinfoid = $("#managerinfoid").val();
            var jobnumber = $("#jobnumber").val();
            var certificatecode = $("#certificatecode").val();
            var mobilephone = $("#mobilephone").val();
            var organizationInfoId = $("#organizationInfoId").val();
            var loginname = $("#loginname").val();
           
            $.ajax({
                url: "<%=path%>/saveManagerInfo",
                dataType: "json",
                type: 'post',
                data: {
                    "managerinfoid": managerinfoid,
                    "managername": managername,
                    "jobnumber": jobnumber,
                    "certificatecode": certificatecode,
                    "mobilephone": mobilephone,
                    "organizationInfoId": organizationInfoId,
                    "loginname": loginname
                },
                success: function (result) {
                    if (result.state == 1) {
                        swal({
                            title: result.msg,
                            type: "success",
                            confirmButtonText: '确认',
                            confirmButtonColor: '#5bc0de'
                        }, function () {
                            load(1);;
                            $('.close').click();
                        })
                    } else {
                        swal({
                            title: result.msg,
                            type: "error",
                            confirmButtonText: '确认',
                            confirmButtonColor: '#5bc0de'
                        })
                    }
                },
                error: function () {
                    swal({
                        title: result.msg,
                        type: "error",
                        confirmButtonText: '确认',
                        confirmButtonColor: '#5bc0de'
                    })
                }
            })
    	}
    })


    $('#myModal').on('hidden.bs.modal', function () {
        $("#managerinfo")[0].reset();
        $("label.error").remove();
    })

    $('#myModal').on('show.bs.modal', function () {
        if ($("#managerinfo #managerName1").val() == "") {
            $("#myModal #myModalLabel").text("添加管理员信息")
            $("#myModal #managerButton").text("提交")
            $("#managerinfo #loginname").removeAttr('readonly')

        } else {
            $("#myModal #myModalLabel").text("修改管理员信息")
            $("#myModal #managerButton").text("确定")
        }
    })
 	$('#myModal2').on('hidden.bs.modal', function () {
        $("#excelForm")[0].reset();
        $('#importTB').html('');
    })
    //回显
    function editManagerInfo(id) {
        $.ajax({
            url: "<%=path%>/viewManagerInfoDetail",
            dataType: "json",
            type: 'post',
            data: {
                "key": id,
            },
            success: function (result) {
                $("#managerinfo #managerName1").val(result.managerName),
                    $("#managerinfo #jobnumber").val(result.jobNumber),
                    $("#managerinfo #certificatecode").val(result.certificateCode),
                    $("#managerinfo #mobilephone").val(result.mobilePhone),
                    $("#managerinfo #managerinfoid").val(result.managerInfoId),
                    $("#managerinfo #organizationInfoId").val(result.organizationInfoId)
                    $("#managerinfo #loginname").val(result.loginName)
                    $("#managerinfo #loginname").attr('readonly', 'readonly')
                $("#myModal").modal('toggle')
            },
        })

    }

    //删除
    function deleteManagerInfo(id) {

        swal({
                title: "确定删除吗？",
                type: "warning",
                showCancelButton: true,
                confirmButtonColor: "#DD6B55",
                confirmButtonText: "确定删除！",
                cancelButtonText: "取消删除！",
                closeOnConfirm: false,
                closeOnCancel: true
            },
            function (isConfirm) {
                if (isConfirm) {
                    $.ajax({
                        url: "<%=path%>/delManagerInfo",
                        type: 'post',
                        data: {"key": id},
                        dataType: 'json',
                        success: function (data) {
                            if (data.num == 1) {
                                swal({
                                        title: "删除成功",
                                        type: "success",
                                        confirmButtonText: '确认',
                                        confirmButtonColor: '#5bc0de',
                                    },
                                    function () {
                                       load(1);
                                    });
                            } else {
                                swal({
                                        title: "删除失败",
                                        type: "error",
                                        confirmButtonText: '确认',
                                        confirmButtonColor: '#5bc0de',
                                    },
                                    function () {
                                        load(1);
                                    });
                            }
                        },
                        error: function () {
                            swal({
                                    title: "删除失败",
                                    type: "error",
                                    confirmButtonText: '确认',
                                    confirmButtonColor: '#5bc0de',
                                },
                                function () {
                                    load(1);
                                });
                        }
                    });
                }
            });

    }

	
    //重置密码
    function resetPassword(id) {
        swal({
                title: "确定重置密码吗？",
                type: "warning",
                showCancelButton: true,
                confirmButtonColor: "#DD6B55",
                confirmButtonText: "确定重置！",
                cancelButtonText: "取消重置！",
                closeOnConfirm: false,
                closeOnCancel: true
            },
            function (isConfirm) {
                if (isConfirm) {
                    $.ajax({
                        url: "<%=path%>/resetPassword",
                        type: 'post',
                        data: {"key": id},
                        dataType: 'json',
                        success: function (data) {
                            if (data.num == 1) {
                                swal({
                                        title: "重置成功",
                                        type: "success",
                                        text: "重置密码为：<%=MyConstant.DEFAULT_PASSWORD%>", 
                                        confirmButtonText: '确认',
                                        confirmButtonColor: '#5bc0de',
                                    });
                            } else {
                                swal({
                                        title: "重置失败",
                                        type: "error",
                                        confirmButtonText: '确认',
                                        confirmButtonColor: '#5bc0de',
                                    });
                            }
                        },
                        error: function () {
                            swal({
                                    title: "重置失败",
                                    type: "error",
                                    confirmButtonText: '确认',
                                    confirmButtonColor: '#5bc0de',
                                });
                        }
                    });
                }
            });

    }
    
    function load(curr) {
        var pageSize = 30; //每页出现的数量
        $.ajax({
            url: "<%=path%>/getManagerInfoPage",
            timeout: 300000,
            dataType: "json",
            type: "post",
            data: {
                "curr": curr,
                "pageSize": pageSize,
                "managerName": $("#managerName").val(),
                "certificateCode": $("#certificateCode").val()
            },
            success: function (data) {
                var html = "";
                $.each(data.list, function (i, item) {
                    var id = item.managerInfoId
                    var editid = "";
                    editid += "&nbsp;&nbsp;&nbsp;&nbsp;";
                    editid = "<a href='javascript:;' class='fa fa-edit' onclick=\"editManagerInfo('" + id + "')\" title=''>编辑</a>";
                    editid += "&nbsp;&nbsp;&nbsp;&nbsp;";
                    editid += "<a href='javascript:;' class='fa fa-trash-o' onclick=\"deleteManagerInfo('" + id + "')\" title='删除'>删除</a>";
                    editid += "&nbsp;&nbsp;&nbsp;&nbsp;";
                    editid += "<a href='javascript:;' class='fa fa-pencil' onclick=\"resetPassword('" + id + "')\" title='重置密码'>重置密码</a>";
                    var n = i + 1;
                    html += " <tr> " +
                        " <td>" + n + "</td> " +
                        " <td>" + item.managerName + "</td> " +
                        " <td>" + item.certificateCode + "</td> " +
                        " <td>" + item.mobilePhone + "</td> " +
                        " <td>" + item.jobNumber + "</td> " +
                        " <td>" + item.organizationName + "</td> " +
                        " <td>" + item.createDate + "</td> " +
                        " <td>" + editid + "</td> " +
                        " </tr>";
                })
                $("#tbody").html(html);
                laypage({
                    cont: 'page', //容器。值支持id名、原生dom对象，jquery对象。【如该容器为】：<div id="page1"></div>
                    pages: Math.ceil(data.totalCount / 30), //通过后台拿到的总页数
                    skin: "#49afcd",
                    curr: curr || 1, //当前页
                    jump: function (obj, first) { //触发分页后的回调
                        if (!first) { //点击跳页触发函数自身，并传递当前页：obj.curr
                            curr = obj.curr;
                            load(curr);
                        }
                    }
                });

            }
        })
    }

    function initData() {
        $.ajax({
            url: '<%=path%>getAllOrganizationInfoExceptRoot',
            type: 'get',
            success: function (result) {
                $(result).each(function (index, item) {
                    $("#myModal select").append('<option value=' + item.organizationInfoId + '>' + item.organizationName + '</option>')
                })
            },
            error: function () {

            }
        })
    }
    function managerImport(){
    	var file = document.getElementById('excelFile');
        var fileName=file.value;    
        if(fileName=="")
        {
        	swal("提交失败", "请选择EXCEL文件", "error");
        	return false;
        }
    	while (fileName.indexOf("\\") != -1)
    	{
        fileName = fileName.slice(fileName.indexOf("\\") + 1);
    	}
        var ext = fileName.slice(fileName.indexOf(".")).toLowerCase();
        if(ext.indexOf("xls") == -1)
        {
        	swal("提交失败", "请上传格式为Excel的文件", "error");
        	return false;
        }
        $("#importButton").replaceWith("<img id='importImg' src='<%=path%>/assets/img/loading.gif' ></img>");
        $("#excelForm").ajaxSubmit(function(data) { 
        	if(data.errorMsg!=null&&data.errorMsg!=''){
        		swal({   
    				  title: "上传失败", 
    				  text: data.errorMsg, 
    				  type: "error",
    				},
    				function(){
    					$("#importImg").replaceWith('<button type="button" id="importButton" class="btn btn-info" onclick="managerImport()">导入模板</button>');
    				});
        	}else{
        		var tbStr="";
        		for(var i=0;i<data.dataList.length;i++){
        			tbStr+="<tr>";
        			if (typeof(data.dataList[i].rowNo) == "undefined" ){
        				tbStr+="<td></td>";
        			}else{
        				tbStr+="<td>"+data.dataList[i].rowNo+"</td>";
        			}
    				if (typeof(data.dataList[i].col0) == "undefined" ){
    					tbStr+="<td></td>";
        			}else{
        				tbStr+="<td>"+data.dataList[i].col0+"</td>";
        			}
    				if (typeof(data.dataList[i].col1) == "undefined" ){
    					tbStr+="<td></td>";
    				}else{
    					tbStr+="<td>"+data.dataList[i].col1+"</td>";
    				}
    				if (typeof(data.dataList[i].col2) == "undefined" ){
    					tbStr+="<td></td>";
    				}else{
    					tbStr+="<td>"+data.dataList[i].col2+"</td>";
    				}
    				if (typeof(data.dataList[i].col3) == "undefined" ){
    					tbStr+="<td></td>";
    				}else{
    					tbStr+="<td>"+data.dataList[i].col3+"</td>";
    				}
    				if (typeof(data.dataList[i].col4) == "undefined" ){
    					tbStr+="<td></td>";
    				}else{
    					tbStr+="<td>"+data.dataList[i].col4+"</td>";
    				}
    				if(managerType=='1001'){
    					if (typeof(data.dataList[i].col5) == "undefined" ){
        					tbStr+="<td></td>";
        				}else{
        					tbStr+="<td>"+data.dataList[i].col5+"</td>";
        				}
    				}
    				if (typeof(data.dataList[i].msg) == "undefined" ){
    					tbStr+="<td></td>";
    				}else{
    					tbStr+="<td>"+data.dataList[i].msg+"</td>";
    				}
        			tbStr+="</tr>";
        		}
        		$("#importTB").html(tbStr);
				$("#importImg").replaceWith('<button type="button" id="importButton" class="btn btn-info" onclick="managerImport()">导入模板</button>');
                load(1);
        	}
    	});
        
        
        
        
    }
</script>

</html>
